package com.hadron.ui.scroll.behaviors 
{
	import com.greensock.TweenMax;
	import com.hadron.ui.scroll.ScrollBox;
	import com.hadron.ui.scroll.events.ScrollbarEvent;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import temple.behaviors.AbstractBehavior;


	/**
	 * ...
	 * @author Hadron
	 */
	public class ScrollBoxBehavior extends AbstractBehavior implements IScrollBoxBehavior 
	{
		/*----------------------- PRIVATE -----------------------*/
		private var _debug:Boolean = false;
		/*------------------------------------------ CONSTRUCTOR --------------------------------------------*/
		public function ScrollBoxBehavior(target:Object) 
		{
			super(target);
			scrollBox.addEventListener(Event.ADDED_TO_STAGE, handleAdd);
			scrollBox.addEventListener(Event.REMOVED_FROM_STAGE, handleRemove);
			scrollBox.addEventListener(MouseEvent.MOUSE_OVER, handleMouseOver);
			scrollBox.addEventListener(MouseEvent.MOUSE_OUT, handleMouseOut);
		}
		
		/* INTERFACE temple.debug.IDebuggable */
		/**
		 * @inheritDoc
		 */
		public function get debug():Boolean { return _debug; }
		/**
		 * @inheritDoc
		 */
		public function set debug(value:Boolean):void { _debug = value;	}
		/*------------------------------------------ PUBLIC METHODS --------------------------------------------*/
		public function get scrollBox():ScrollBox
		{
			return target as ScrollBox;
		}
		
		public function updateContainerPosition(percent:Number):void
		{
			TweenMax.to(scrollBox.target, scrollBox.timing, { y:(scrollBox.containerOriginY - percent * (scrollBox.target.height - scrollBox.masker.height)),
											ease:scrollBox.transition} );
		}
		/**
		 * @inheritDoc
		 */
		override public function destruct():void
		{
			if (debug) logDebug('destructing scrollbox behavior');
			//remove all listeners
			if (scrollBox) {
				scrollBox.removeEventListener(Event.ADDED_TO_STAGE, handleAdd);
				scrollBox.removeEventListener(Event.REMOVED_FROM_STAGE, handleRemove);
				scrollBox.removeEventListener(MouseEvent.MOUSE_OVER, handleMouseOver);
				scrollBox.removeEventListener(MouseEvent.MOUSE_OUT, handleMouseOut);
			}
			super.destruct();
		}
		
		/*------------------------------------------ EVENT HANDLERS --------------------------------------------*/
		private function handleMouseOver(e:MouseEvent):void	{ scrollBox.scrollbar.isMouseOver = true; }
		private function handleMouseOut(e:MouseEvent):void { scrollBox.scrollbar.isMouseOver = false; }
		
		private function handleAdd(e:Event):void 
		{
			//_target.removeEventListener(Event.ADDED_TO_STAGE, handle_targetAdd);
			scrollBox.resetPosition();
			scrollBox.containerOriginY = scrollBox.target.y;
			scrollBox.scrollbar.addEventListener(ScrollbarEvent.VALUE_CHANGED, handleScrollbarChange);
			scrollBox.scrollbar.x = scrollBox.target.x + scrollBox.target.width + scrollBox.spacing;
			//scrollBox.scrollbar.y = (scrollBox.offsetY != 0) ? scrollBox.offsetY : scrollBox.target.y;
			scrollBox.scrollbar.y = scrollBox.target.y;
			scrollBox.checkHeight();
		}
		
		private function handleScrollbarChange(e:ScrollbarEvent):void 
		{
			updateContainerPosition(e.scrollPercent);
		}
		
		private function handleRemove(e:Event):void 
		{
			//_target.removeEventListener(Event.REMOVED_FROM_STAGE, handle_targetRemove);
			scrollBox.scrollbar.removeEventListener(ScrollbarEvent.VALUE_CHANGED, handleScrollbarChange);
			TweenMax.killAll();
		}
	}
}